package com.luck.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

    public static String formatDateTime(Date date) {
        if (date == null) {
            return "--";
        }
        //DateFormat dateFormat = DateFormat.getDateTimeInstance();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    public static String formatDate(Date date) {
        if (date == null) {
            return "--";
        }
        //DateFormat dateFormat = DateFormat.getDateInstance();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(date);
    }

    /**
     * @return 当前时间 - date
     */
    public static long diffDays(Date date) {
        long dayDiff = ChronoUnit.DAYS.between(
                date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(),
                new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()
        );
        return dayDiff;
    }

    /**
     * @return 当前时间 - date
     */
    public static long diffDays(String dateStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date == null) {
            return 0;
        }
        return ChronoUnit.DAYS.between(
                date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(),
                new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()
        );
    }

    /**
     * 返回时间段 单位:日
     * @param begin
     * @param end
     * @return
     */
    public static List<String> returnDateDaySegment(Date begin, Date end) {
        if (end == null) {
            end = begin;
        }
        List<String> dateList = new ArrayList();
        if (begin == null) {
            return dateList;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cStart = Calendar.getInstance();
        cStart.setTime(begin);
        dateList.add(sdf.format(begin));
        while (end.after(cStart.getTime())) {
            cStart.add(Calendar.DAY_OF_MONTH, 1);
            dateList.add(sdf.format(cStart.getTime()));
        }
        return dateList;
    }

    /**
     * 返回时间段 单位:日
     * @param begin
     * @param end
     * @return
     */
    public static List<String> returnDateDaySegment(String begin, String end) {
        if (end == null) {
            end = begin;
        }
        List<String> dateList = new ArrayList();
        if (begin == null) {
            return dateList;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cStart = Calendar.getInstance();
        try {
            cStart.setTime(sdf.parse(begin));
            dateList.add(begin);
            while (sdf.parse(end).after(cStart.getTime())) {
                cStart.add(Calendar.DAY_OF_MONTH, 1);
                dateList.add(sdf.format(cStart.getTime()));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateList;
    }

    /**
     * 返回时间段 单位:月
     * @param begin
     * @param end
     * @return
     */
    public static List<String> returnDateMonthSegment(Date begin, Date end) {
        if (end == null) {
            end = begin;
        }
        List<String> dateList = new ArrayList();
        if (begin == null) {
            return dateList;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar cStart = Calendar.getInstance();
        cStart.setTime(begin);
        dateList.add(sdf.format(begin));
        while (end.after(cStart.getTime())) {
            cStart.add(Calendar.MONTH, 1);
            dateList.add(sdf.format(cStart.getTime()));
        }
        return dateList;
    }

    /**
     * 返回时间段 单位:月
     * @param begin
     * @param end
     * @return
     */
    public static List<String> returnDateMonthSegment(String begin, String end) {
        if (end == null) {
            end = begin;
        }
        List<String> dateList = new ArrayList();
        if (begin == null) {
            return dateList;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar cStart = Calendar.getInstance();
        try {
            cStart.setTime(sdf.parse(begin));
            dateList.add(begin);
            while (sdf.parse(end).after(cStart.getTime())) {
                cStart.add(Calendar.MONTH, 1);
                dateList.add(sdf.format(cStart.getTime()));
            }
        } catch (ParseException e) {
                e.printStackTrace();
        }
        return dateList;
    }

    public static List<String> returnMinuteSegment(Object day, int segment) {
        List<String> dateList = new ArrayList();
        if (day == null) {
            return dateList;
        }
        Date someDay;
        if (day instanceof Date) {
            someDay = (Date) day;
        } else if (day instanceof String){
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                someDay = sdf.parse((String) day);
            } catch (ParseException e) {
                return dateList;
            }

        } else {
            return dateList;
        }
        DateFormat sdf = new SimpleDateFormat("HH:mm");
        Calendar cStart = Calendar.getInstance();
        cStart.setTime(someDay);
        cStart.set(Calendar.HOUR_OF_DAY, 0);
        cStart.set(Calendar.MINUTE, 0);
        cStart.set(Calendar.SECOND, 0);
        cStart.set(Calendar.MILLISECOND, 0);
        dateList.add(sdf.format(cStart.getTime()));
        Calendar cEnd = Calendar.getInstance();
        cEnd.setTime(cStart.getTime());
        cEnd.add(Calendar.DAY_OF_MONTH, 1);
        while (cEnd.getTime().after(cStart.getTime())) {
            cStart.add(Calendar.MINUTE, segment);
            dateList.add(sdf.format(cStart.getTime()));
        }
        String lastDate = dateList.get(dateList.size() - 1);
        if ("00:00".equalsIgnoreCase(lastDate)) {
            dateList.set(dateList.size() - 1, "24:00");
        }
        return dateList;
    }
    public static List<String> returnDateMinuteSegment(Object day, int segment) {
        List<String> dateList = new ArrayList();
        if (day == null) {
            return dateList;
        }
        Date someDay;
        if (day instanceof Date) {
            someDay = (Date) day;
        } else if (day instanceof String){
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                someDay = sdf.parse((String) day);
            } catch (ParseException e) {
                return dateList;
            }

        } else {
            return dateList;
        }
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Calendar cStart = Calendar.getInstance();
        cStart.setTime(someDay);
        cStart.set(Calendar.HOUR_OF_DAY, 0);
        cStart.set(Calendar.MINUTE, 0);
        cStart.set(Calendar.SECOND, 0);
        cStart.set(Calendar.MILLISECOND, 0);
        dateList.add(sdf.format(cStart.getTime()));
        Calendar cEnd = Calendar.getInstance();
        cEnd.setTime(cStart.getTime());
        cEnd.add(Calendar.DAY_OF_MONTH, 1);
        while (cEnd.getTime().after(cStart.getTime())) {
            cStart.add(Calendar.MINUTE, segment);
            dateList.add(sdf.format(cStart.getTime()));
        }
        return dateList;
    }

}
